package com.catena.web.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;


/**
 * Created by hx-pc on 16-11-1.
 */
public abstract class WebSocketPool extends Pool<Session> {

    protected Logger LOGGER = LoggerFactory.getLogger(getClassName());
    private static int onlineCount = 0;

    protected Class getClassName() {
        return this.getClass();
    }

    public void onOpen(Session session) {
        this.append(session);
        addOnlineCount();
        LOGGER.info("有新链接加入!当前在线人数为{}", getOnlineCount());
    }

    public void onClose(Session session) {
        this.remove(session);
        subOnlineCount();
        LOGGER.info("有一链接关闭!当前在线人数为{}", getOnlineCount());
    }

    public void onMessage(String message){
        LOGGER.info("来自客户端的消息:{}", message);
    }

    public int getOnlineCount() {
        return onlineCount;
    }

    public synchronized void addOnlineCount() {
        onlineCount++;
    }

    public synchronized void subOnlineCount() {
        onlineCount--;
    }

}
